; 这里不需要提供对_BOOT_DEBUG的支持
org 07c00h
BaseOfStack equ 07c00h

BaseOfLoadFile equ 09000h
OffsetOfLoadFile equ 0100h

jmp LABEL_START
nop

%define _IN_BOOT_ 1

; 把boot和loader共同的头包含进来，这里面包含FAT12的header
%include "common_head.asm"

LABEL_START:
	mov ax, cs
	mov ds, ax
	mov ss, ax
	mov sp, BaseOfStack
	
	; 清屏
	mov ax, 0600h
	mov bx, 0700h
	mov cx, 0
	mov dx, 0184fh
	int 10h
	
	; 调用显示字符串函数，显示Booting
	push BootMessage
	mov dh, 0
	call DispStr
	add sp, 2
	
	; 软驱复位
	xor ah, ah
	xor al, al
	int 13h
	
	push LoaderFileName
	call findInRootDir
	add sp, 2
	
	cmp ax, 0
	jz LABEL_FILE_FOUND	
LABEL_FILE_NOT_FOUND:
	; 如果加载失败，以后再处理
	; 为了省下空间，将此处直接注掉了，反正也走不到
	; jmp $
	
LABEL_FILE_FOUND:
	and di, 0FFE0h
	add di, 01Ah
	mov cx, word [es:di]
	push cx
	call ReadFileByFirstSector
	add sp, 2
	
	cmp ax, 0
	jz loadSuccess
	; 如果加载失败，以后再处理
	; 为了省下空间，将此处直接注掉了，反正也走不到
	; jmp $
loadSuccess:
	mov dh, 1
	push Message1
	call DispStr
	add sp, 2
	jmp BaseOfLoadFile:OffsetOfLoadFile
	
wSectorNo dw 0
bOdd db 0

LoaderFileName db "LOADER  BIN"
BootMessage: db "Booting  "
BootMessageLength equ $ - BootMessage
Message1: db "Ready.   "
Message1Length equ $ - Message1
;Message2: db "NO LOADER"
;Message2Length equ $ - Message2

%include "common_funcs.asm"

times 510 - ($ - $$) db 0
dw 0xaa55